// pages/login/index.js
const common = require('../../public.js')
const config = require('../../config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog:false,
    checkList: [
        {value: '', name: '',checked: false},
      ],
    agreeLogin:false,
    // agreeChecked:false,
    showPrivacy: false,
    loginLogo: config.loginLogo,
    captchaId: 'fb6455d8a495496f8780b3c0aed5ea85',
    loginFlag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, "options")
    //获得胶囊位置，设置返回按钮
    const capsule = wx.getMenuButtonBoundingClientRect()
    this.setData({
      topOffset: capsule.top,
      height: capsule.height,
      gobackUrl: options.backUrl
    })
    if (options.backUrl) {
      this.data.backUrl = decodeURIComponent(options.backUrl)
    }
    console.log(this.data.backUrl, "测试首页")
    if (this.data.backUrl) {
      if (this.data.backUrl.indexOf('/pages/myNian/payCard/payCard?cardId') == 0) {
        this.setData({
          loginFlag: true
        })
      }
    }
    // setTimeout(()=>{
    //   this.login()
    // },1000)
  },
  checkboxChange(e) {
      var that = this;
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    that.setData({

        agreeLoginCanClick:that.data.agreeLoginCanClick==true?false:true,
    })

  },
//   onAgreeChange(event) {
//     this.setData({
//       agreeChecked: event.detail,
//       agreeLoginCanClick:event.detail
//     });
//   },
  login() {
      if(!this.data.agreeLoginCanClick){
        this.setData({
            showDialog:true
        })
        // common.showToast('请勾同意按钮')
            return;
      }
      this.setData({
          agreeLogin:true,
      })
    // console.log("登录")
    const that = this
    // this.selectComponent('#captcha').popup() //验证码登录
    // 调用登录方法，这里需要传入返回的url //原来的
    common.loginFunc(this.data.backUrl).then(function (loginSucessed) {
        console.log('loginSucessed----',loginSucessed);
      if (loginSucessed) {
        that.popPage(true)
      } else {
        const backUrl = config.myCenterUrl ? '/' + config.myCenterUrl : '/pages/mySelf/Index/index'
        wx.redirectTo({
          url: backUrl,
          success() {
            common.showToast('登录失败')
          }
        })
      }
    })
  },

  // // 验证码验证结果回调
  handleCaptchaVerify: function (ev) {
    var that = this
    if (!ev.detail[0]) {
      // 验证成功
      common.loginFunc(this.data.backUrl, ev.detail[1]).then(function (loginSucessed) {
        if (loginSucessed) {
          that.popPage(true)
        } else {
          const backUrl = config.myCenterUrl ? '/' + config.myCenterUrl : '/pages/mySelf/Index/index'
          wx.redirectTo({
            url: backUrl,
            success() {
              common.showToast('登录失败')
            }
          })
        }
      }, _ => {
        that.selectComponent('#captcha').reset()
      })
    } else {
      // 验证失败
      wx.showToast({
        title: '验证失败',
        icon: 'success',
        duration: 2000
      })
    }
  },

  // 返回指定界面
  popPage(showToast = false) {
    const that = this
    console.log(that.data.backUrl, "页面路径")
    if (that.data.backUrl) {
      wx.redirectTo({
        url: that.data.backUrl,
        success() {
          showToast && common.showToast('登录成功')
        },
        fail() {
          // tabar页面
          wx.redirectTo({
            url: that.data.backUrl,
          })
        }

      })
    } else if (that.data.backUrl == 'pages/login/index') {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    } else {
      let pages = getCurrentPages();
      let prevpage = pages[pages.length - 2];
      console.log(prevpage,"prevpage")
      if (prevpage && prevpage.route && prevpage.route == "pages/sys/sys") {
        wx.redirectTo({
          url: '/pages/index/index',
        })
        return
      } else {
        wx.redirectTo({
          url: '/pages/index/index',
          success() {
            showToast && common.showToast('登录成功')
          }
        })
        return
      }
    }
  },
  cancel() {
    var that = this
    var dealbackUrl = that.data.backUrl ? that.data.backUrl.replace(/\"/g, "") : ''
    console.log(dealbackUrl)
    if (dealbackUrl.indexOf('/pages/myNian/payCard/payCard?cardId') == 0) {

      this.setData({
        loginFlag: true
      })
      common.showToast('购买会员卡，请先快捷登录')
      return
    } else {
      wx.navigateTo({
        url: '/pages/login/phone?backUrl=' + this.data.gobackUrl,
      })
    }

  },
  cancels() {
    var that = this
    var dealbackUrl = that.data.backUrl ? that.data.backUrl.replace(/\"/g, "") : ''
    console.log(dealbackUrl)
    if (dealbackUrl.indexOf('/pages/myNian/payCard/payCard?cardId') == 0) {

      this.setData({
        loginFlag: true
      })
      common.showToast('购买会员卡，请先快捷登录')
      return
    } else {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }


  },
  showPrivacy() {
    this.setData({
      showPrivacy: true
    })
  },
  closePrivacy() {
    this.setData({
      showPrivacy: false
    })
  },
  agreeCommit(){
      console.log('同意点击');
    
      this.data.checkList[0].checked = true;
      this.setData({
        checkList:this.data.checkList,
        agreeLoginCanClick:true
      })
  }
})